/// 商品模型 - Product model
class Product {
  /// 商品ID - Product ID
  final String id;
  
  /// 商品名称 - Product name
  final String name;
  
  /// 商品描述 - Product description
  final String description;
  
  /// 商品图片 - Product image
  final String image;
  
  /// 商品价格 - Product price
  final double price;
  
  /// 原价 - Original price
  final double originalPrice;
  
  /// 分类ID - Category ID
  final String categoryId;
  
  /// 是否为推荐商品 - Whether it's a featured product
  final bool isFeatured;
  
  /// 是否激活 - Whether it's active
  final bool isActive;
  
  /// 是否可用 - Whether it's available
  final bool isAvailable;
  
  /// 库存数量 - Stock quantity
  final int stock;
  
  /// 销售数量 - Sales count
  final int salesCount;
  
  /// 评分 - Rating
  final double rating;
  
  /// 评论数量 - Review count
  final int reviewCount;
  
  /// 标签列表 - Tags list
  final List<String> tags;
  
  /// 创建时间 - Created at
  final DateTime createdAt;
  
  /// 更新时间 - Updated at
  final DateTime updatedAt;

  const Product({
    required this.id,
    required this.name,
    required this.description,
    required this.image,
    required this.price,
    required this.originalPrice,
    required this.categoryId,
    this.isFeatured = false,
    this.isActive = true,
    this.isAvailable = true,
    this.stock = 0,
    this.salesCount = 0,
    this.rating = 0.0,
    this.reviewCount = 0,
    this.tags = const [],
    required this.createdAt,
    required this.updatedAt,
  });

  /// 从JSON创建Product实例 - Create Product instance from JSON
  factory Product.fromJson(Map<String, dynamic> json) {
    return Product(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      description: json['description'] ?? '',
      image: json['image'] ?? '',
      price: (json['price'] ?? 0).toDouble(),
      originalPrice: (json['original_price'] ?? 0).toDouble(),
      categoryId: json['category_id'] ?? '',
      isFeatured: json['is_featured'] ?? false,
      isActive: json['is_active'] ?? true,
      isAvailable: json['is_available'] ?? true,
      stock: json['stock'] ?? 0,
      salesCount: json['sales_count'] ?? 0,
      rating: (json['rating'] ?? 0).toDouble(),
      reviewCount: json['review_count'] ?? 0,
      tags: json['tags'] != null 
          ? List<String>.from(json['tags']) 
          : [],
      createdAt: json['created_at'] != null 
          ? DateTime.parse(json['created_at']) 
          : DateTime.now(),
      updatedAt: json['updated_at'] != null 
          ? DateTime.parse(json['updated_at']) 
          : DateTime.now(),
    );
  }

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'image': image,
      'price': price,
      'original_price': originalPrice,
      'category_id': categoryId,
      'is_featured': isFeatured,
      'is_active': isActive,
      'is_available': isAvailable,
      'stock': stock,
      'sales_count': salesCount,
      'rating': rating,
      'review_count': reviewCount,
      'tags': tags,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }
}